package cn.gson.springboot.controller.productcontroller;

import cn.gson.springboot.model.pjos.*;
import cn.gson.springboot.model.service.productservice.ProductInfoService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Controller
public class productInfoController {

    @Autowired
    ProductInfoService productInfoService;


    /**
     * 产品详情
     * @param psId
     * @return
     */
    @RequestMapping("/product_main")
    @ResponseBody
    public Zproductside productMain(Integer psId){
        Zproductside zproductside = productInfoService.productMain(psId);
        return zproductside;

    }

    /**
     * 产品列表
     * @return
     */
    @RequestMapping("/product_info")
    @ResponseBody
    public Map<String,Object> productsList(Integer page,Integer size,Integer productTpye,String productName){
        System.out.println("page"+page);
        System.out.println("size"+size);
        System.out.println("productTpye"+productTpye);
        System.out.println("productName"+productName);
        Page<Object> pageinfo= PageHelper.startPage(page,size);
        List<Products> products = productInfoService.productsList(productTpye,productName);
        Map<String,Object> map = new HashMap<>();
        map.put("total",pageinfo.getTotal());
        map.put("rows",pageinfo.getResult());
        return map;
    }


    @RequestMapping("/Productbrand")
    @ResponseBody
    public List<Productbrand>ProductbrandList(){
        List<Productbrand> productbrands = productInfoService.ProductbrandList();
        return productbrands;
    }


    @RequestMapping("/newproduct_info")
    @ResponseBody
    public  List<Zproduct> zproductList(){
        List<Zproduct> zproducts = productInfoService.zproductList();
       /* Page<Object> pageinfo= PageHelper.startPage(page,size);*/
      /*  Map<String,Object> map = new HashMap<>();
        map.put("total",pageinfo.getTotal());
        map.put("rows",pageinfo.getResult());*/
        return zproducts;
    }


    /***
     * 新代码
     */

    @RequestMapping("/zspecifications_info")
    @ResponseBody
    public List<Zspecifications> zspecificationsList(Integer id){
        List<Zspecifications> zspecifications = productInfoService.zspecificationsList(id);
        return  zspecifications;
    }


    @RequestMapping("/add_info")
    @ResponseBody
    public void addZspInfo(Integer specId,String specVlaue){
        Zspecificationsvalue zspecificationsvalue = new Zspecificationsvalue();
        Zspecifications zspecifications = productInfoService.ZspeFindById(specId);
        zspecificationsvalue.setSpecId(0);
        zspecificationsvalue.setSpecVlaue(specVlaue);
        zspecificationsvalue.setZspecificationsByOnsId(zspecifications);
        productInfoService.addZsp(zspecificationsvalue);
    }

    @RequestMapping("/add_zspecifications")
    @ResponseBody
    public void addZspecifications(Integer id,String zspeName,String zspeValue){
       productInfoService.addZspecificationsinfo(id,zspeName,zspeValue);

        System.err.println("kpid"+id);
        System.err.println("zspeName"+zspeName);
        System.err.println("zspeValue"+zspeValue);
    }


    @RequestMapping("/add_Productzhao")
    @ResponseBody
    public Integer addProductInfo(String productInfo,String info){
        Integer integer = productInfoService.productModelSave(productInfo, info);
      /*  Object o = JSONObject.toJSON(info);
        JSONObject jsonx = JSON.parseObject(info);
        Zproduct zproduct = new Zproduct();

        Integer pcId = jsonx.getInteger("pcId");
        Zproductcategory info1 = productInfoService.getInfo(1);//类型
        Integer pdId = jsonx.getInteger("pdId");
        Zproductbrand brandInfo = productInfoService.getBrandInfo(pdId);//品牌
        String name = jsonx.getString("name");
        String model = jsonx.getString("model");
        BigDecimal factoryPrice = jsonx.getBigDecimal("factoryPrice");
        BigDecimal price = jsonx.getBigDecimal("price");
        String imgpath = jsonx.getString("imgpath");
        String unit = jsonx.getString("unit");
        Integer number = jsonx.getInteger("number");

        //产品主表(缺主主图片)
        zproduct.setPrImgpath("未获取");
        zproduct.setProductName(name);
        zproduct.setPrModel(model);
        zproduct.setZproductbrandByPdId(brandInfo);
        zproduct.setZproductcategoryByPcId(info1);
        String s = zproduct.toString();
        System.out.println("产品主表信息"+s);



        //产品副标(缺时间)
        Zproductside zproductside = new Zproductside();
        java.util.Date utilDate = new java.util.Date();//util.Date
        //util.Date转sql.Date
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

        zproductside.setPsPrice(price);
        zproductside.setPsNumber(number);
        zproductside.setPsState(1);
        zproductside.setPsUnit(unit);
        zproductside.setPsFactoryPrice(factoryPrice);
        zproductside.setZproductByProductId(zproduct);

        System.err.println("规格信息"+productInfo);

        String skh ="(\\d+)";
        Pattern pattern = Pattern.compile(skh);
        Matcher matcher = pattern.matcher(productInfo);
        String tpl=null;
        while (matcher.find()){
            String group = matcher.group();

        }

       *//* String[] split = s1.split(",");
        for(String value:split){
            System.out.println("风格"+value);
        }*//*
*/
        return integer;
    }



    //多个文件的上传
    @RequestMapping("/product_upload")
    @ResponseBody
    public void uploads(@RequestParam("file")MultipartFile[] uploadFiles ,Integer returnid) {
       System.out.println("都文件"+returnid);
        System.out.println("图片资源"+uploadFiles.length);

        String [] imgPath= new String[uploadFiles.length];
        //2，定义文件的存储路径,
        String uri = "E:/upload";
        File dir = new File(uri);
        if (!dir.isDirectory()) {//文件目录不存在，就创建一个
            dir.mkdirs();
        }

        String filePathS = "";
        //3，遍历文件数组，一个个上传
        for (int i = 0; i < uploadFiles.length; i++) {
            System.out.println("小六在"+i);
            MultipartFile file = uploadFiles[i];
            String fileName = file.getOriginalFilename();
            String ext = FilenameUtils.getExtension(fileName);
            //生成一个新的名字
            String newName = UUID.randomUUID().toString();
            String filePath = newName + "." + ext;
            imgPath[i]="http://localhost:8088/"+"upload/"+filePath;
            File f = new File(uri,filePath);
            try {
                //文件转移
                file.transferTo(f);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        Zproductimgs zproductimgs = new Zproductimgs();
        zproductimgs.setImgsId(0);
        zproductimgs.setImgsaSrc(imgPath[0]);
        zproductimgs.setImgsbSrc(imgPath[1]);
        zproductimgs.setImgscSrc(imgPath[2]);
        Integer integer = productInfoService.imgPath(zproductimgs);


        productInfoService.imgPathSave(imgPath,returnid,integer);

    }

}
